/**    
 * @{#} MyFollowingDoctorActivity.java Create on 2014-7-8 下午2:49:08    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import cn.skinapp.R;

import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.evan.common.pullrefresh.PullToRefreshBase;
import com.evan.common.pullrefresh.PullToRefreshBase.OnRefreshListener;
import com.evan.common.pullrefresh.PullToRefreshListView;
import com.smiier.skin.adapter.QuestionAdapter;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class MyFollowingQuestionActivity extends BasicActivity implements
		OnRefreshListener<ListView>, OnItemClickListener {

	private PullToRefreshListView list_following_question;
	// private TextView text_empty;
	private QuestionAdapter questionAdapter;
	String start_qid = "";
	HashMap<String, Object> params = new HashMap<String, Object>(),
			params_ = new HashMap<String, Object>();
	ArrayList<Object> array = new ArrayList<Object>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myfollowing_doctor);

		init();
		setNavTitle("我的收藏");
		setNavLeftBtn("我的");
		list_following_question = (PullToRefreshListView) findViewById(R.id.list_myfollowing);
		list_following_question.setOnRefreshListener(this);
		list_following_question.setPullRefreshEnabled(false);
		list_following_question.setScrollLoadEnabled(true);

		// text_empty = (TextView) findViewById(R.id.text_empty);
		// text_empty.setText("尚无关注的问题");

		params.put(Constant.PARAM_API, "Follow.GetList");
		params.put("pageSize", Constant.pageSize);
		params.put("type", 2);
		params.put(Constant.PARAM_TOKEN, GlobalSettings.sToken);

		params_.put(Constant.PARAM_API, "Question.Get");
		params_.put(Constant.PARAM_TOKEN, GlobalSettings.sToken);

		list_following_question.doPullRefreshing(true);
		list_following_question.setOnItemClickListener(this);
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		loadData(false);
	}

	@Override
	public void onPullUpToLoadMore(PullToRefreshBase<ListView> refreshView) {
		loadData(false);
	}

	public void loadData(boolean isShowDialog) {
		params.put("startID", start_qid);
		RequestTaskIntercept.requestIntercept(activity, Constant.URL.REQUEST,
				new RequestHandler(activity, new HandlerCallback() {

					@Override
					public void callback(Object o) {
						if (!CommonUtility.isNull(o)) {
							try {
								JSONObject object = (JSONObject) o;
								if (CommonUtility.response200(object)) {
									object = object.getJSONObject("Res");
									JSONArray arr = object
											.getJSONArray("Follows");
									String ids = "";
									for (int i = 0; i < arr.length(); i++) {
										object = arr.getJSONObject(i);
										ids += object.getString("Following_Id");
										if (i < arr.length() - 1) {
											ids += ",";
										}
									}
									if (arr.length() > 0) {
										start_qid = arr.getJSONObject(
												arr.length() - 1).getString(
												"Following_Id");
									}
									if (!CommonUtility.isNull(ids)) {
										params_.put("qids", ids);
										RequestTaskIntercept.requestIntercept(
												activity, Constant.URL.REQUEST,
												new RequestHandler(activity,
														new HandlerCallback() {

															@Override
															public void callback(
																	Object o) {
																if (!CommonUtility
																		.isNull(o)) {
																	list_following_question
																			.doComplete();
																	try {
																		JSONObject object = (JSONObject) o;
																		JSONArray user_array = object
																				.getJSONArray(Constant.JSON_PARAM_RES);
																		if (user_array
																				.length() > 0) {
																			CommonUtility
																					.putAll(array,
																							user_array,
																							true);
																			if (CommonUtility
																					.isNull(questionAdapter)) {
																				questionAdapter = new QuestionAdapter(
																						activity,
																						array);
																				list_following_question
																						.setAdapter(questionAdapter);
																			} else {
																				questionAdapter
																						.notifyUpdate();
																			}
																			if (user_array
																					.length() < Constant.pageSize) {
																				list_following_question
																						.setHasMoreData(false);
																			} else {
																				list_following_question
																						.setHasMoreData(true);
																			}
																		} else {
																			list_following_question
																					.setHasMoreData(false);
																		}

																	} catch (Exception e) {
																		e.printStackTrace();
																	}
																}
															}
														}), false, params_);
									} else {
										if (array.size() < 1) {
											// text_empty
											// .setVisibility(View.VISIBLE);
										} else {
											list_following_question
													.setHasMoreData(false);
										}
										list_following_question.doComplete();
									}
								} else {
									CommonUtility.toast(activity,
											R.string.server_unavailable);
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}), isShowDialog, params);
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int arg2,
			long arg3) {
		JSONObject object = (JSONObject) view.getTag(R.id.tag_obj);
		Intent intent = new Intent(activity, QuestionDetailActivity.class);
		intent.putExtra(Constant.IDENTITY_KEY, object.toString());
		startActivity(intent);
	}
	
/*	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
